/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.gar.converters;

import edu.upn.sigemfi.gar.be.Notaria;
import edu.upn.sigemfi.gar.beans.AbstractController;
import edu.upn.sigemfi.gar.beans.NotariaController;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Freddy Paz Espinoza
 */
public class NotariaConverter implements Converter {

    public NotariaConverter() {
    }

    public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) throws ConverterException {
        if (string == null) {
            return null;
        }
        try {
            Long idNotaria = Long.parseLong(string);
            return ((NotariaController) AbstractController.getBean("NotariaController")).buscar(idNotaria);
        } catch (Exception e) {
            return null;
        }
    }

    public String getAsString(FacesContext arg0, UIComponent arg1, Object object) throws ConverterException {
        if (object == null) {
            return null;
        }
        if (object instanceof Notaria) {
            return ((Notaria) object).getIdNotaria().toString();
        } else {
            throw new IllegalArgumentException("Tipo de dato incorrecto. Se esperaba be.Notaria");
        }
    }
}
